import {Result} from '@/base/Result';
import {request} from '@/base/Request';
import {Page} from '@/base/Page';
import qs from 'qs'
import {Coupons} from '@/base/model/Coupons';
import {MemberCoupon} from '@/base/model/MemberCoupon';

export function getCouponsList(params: {
  pageIndex: number,
  pageSize: number,
  sortDirection: string;
  couponName?: string,
  createBegin?: string,
  createEnd?: string
}): Promise<Result<Page<Coupons>>> {
  return request.getPage(Coupons, '/api/coupon/fetchCoupons', {params});
}
export function getModelList(): Promise<Result<String>> {
  return request.get(String, '/api/mall/productList')
}
export function postCoupon(params: {
  companyId: number,
  agentId: number,
  couponName: string,
  condition: number,
  reduce: number,
  daynum?: number,
  startTime?: String,
  memberRadio: number,
  targetAccounts: [],
  targetAccountsIn: [],
  targetAccountsOut: [],
  productRadio: number,
  targetProducts: []
}) {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Coupons, `api/coupon/add`, formData);
}
export function postCouponV2(params: {
  companyId: number,
  agentId: number,
  couponName: string,
  condition: number,
  reduce: number,
  daynum?: number,
  startTime?: String,
  memberRadio: number,
  targetAccounts: [],
  targetAccountsIn: [],
  targetAccountsOut: [],
  productRadio: number,
  targetProducts: []
}) {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Coupons, `api/coupon/addV2`, formData);
}
export function putCoupon(params: {
  couponId: number,
  companyId: number,
  agentId: number,
  couponName: string,
  condition: number,
  reduce: number,
  daynum?: number,
  startTime?: string,
  memberRadio: number,
  targetAccounts: string[],
  targetAccountsIn: string[],
  targetAccountsOut: string[],
  productRadio: number,
  targetProducts: string[]
}) {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Coupons, 'api/coupon/update', formData);
}
export function putCouponV2(params: {
  couponId: number,
  companyId: number,
  agentId: number,
  couponName: string,
  condition: number,
  reduce: number,
  daynum?: number,
  startTime?: string,
  memberRadio: number,
  targetAccounts: string[],
  targetAccountsIn: string[],
  targetAccountsOut: string[],
  productRadio: number,
  targetProducts: string[]
}) {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(Coupons, 'api/coupon/updateV2', formData);
}
export function banCoupon(params:{couponId: number}) {
  return request.post(Coupons, `/api/coupon/delCoupon`, qs.stringify(params));
}
export function deleteCoupon(params:{couponId: number}) {
  return request.post(Coupons, `/api/coupon/delCoupon`, qs.stringify(params));
}
export function getCoupon(params:{couponId: number}): Promise<Result<Coupons>> {
  return request.get(Coupons, `/api/coupon/getCouponsById`, {params});
}
export function getProducts(params:{couponId: number}): Promise<Result<String>> {
  return request.get(String, `/api/coupon/getCouponsProduct`, {params});
}
export function getCouponMemberList(params: {
  pageIndex: number,
  pageSize: number,
  phone?: string,
  companyId?: number[],
  couponId: number
}): Promise<Result<Page<MemberCoupon>>> {
  console.log(params);
  return request.getPage(MemberCoupon, '/api/coupon/fetchMemberCoupon', { params });
}
// 发放优惠券
export function PutCouponId(params: {
  couponId: number
}) {
  console.log(params);
  return request.post(Coupons, `/api/coupon/giveCoupon`, qs.stringify(params))
}
